﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="PmsManager.ascx.cs"
    Inherits="FreshCms.BackEnd.Controls.Systems.Group.PmsManager" %>
<div id="page-content" class="block">
    <div class="block-header">
        <a href="javascript:void(0)" class="header-title-link"><h1>Phân quyền người dùng</h1></a>
    </div>
    <div class="block">
        <div class="block-title">
            <h2>
                <a href="javascript:void(0)" class="btn btn-default btn-xs btn-primary" onclick="fadeData(this, 'tblUserList')"><i class="hi hi-resize-full"></i></a>
                Danh sách người dùng được phân quyền
            </h2>
        </div>
        <div class="table-responsive">
            <table id="tblUserList" class="table table-hover" style="display:none;">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 60px;">#</th>
                        <th class="text-center" style="width: 60px;">Id</th>
                        <th>Tài khoản</th>
                        <th>Tên đầy đủ</th>
                        <th class="text-center" style="width: 200px;">Trạng thái</th>
                    </tr>
                </thead>
                <tbody>
                    <asp:Repeater ID="rptListUser" runat="server">
                        <ItemTemplate>
                            <tr>
                                <td class="text-center"><%# Container.ItemIndex + 1 %></td>
                                <td class="text-center"><%# Eval("UserId")%></td>
                                <td><%# Eval("UserName")%></td>
                                <td><%# Eval("UserFullname")%></td>
                                <td class="text-center"><asp:Label ID="lblStatus" runat="server" Text=""></asp:Label></td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </tbody>
            </table>
        </div>
    </div>
    
    <input type="hidden" value='<%=lstShortPms %>'/>
    <h4 class="sub-header">
        Bảng phân quyền
        <button type="button" id="btnUpdatePms" class="btn btn-sm btn-success"><i class="hi hi-ok mr5"></i>Cập nhật</button>
        <a href="/BackEnd/?ui=2" type="button" id="btnBack" class="btn btn-sm btn-primary"><i class="hi hi-arrow-left"></i> Quay lại</a>
    </h4>
    <table id="tblPms" class="table table-hover" style="display: table;">
        <thead>
            <tr>
                <th class="text-center" style="width: 60px;">#</th>
                <th class="text-center" style="">Tên chức năng</th>
                <th class="text-center" style="width: 100px;">Xem</th>
                <th class="text-center" style="width: 100px;">Sửa</th>
                <th class="text-center" style="width: 100px;">Xóa</th>
                <th class="text-center" style="width: 100px;">Trạng thái</th>
            </tr>
        </thead>
        <tbody>
            <asp:Repeater ID="rpt" runat="server">
                <ItemTemplate>
                    <asp:PlaceHolder ID="phParent" runat="server" Visible="false">
                        <tr class="parent_cate">
                            <td class="text-center"><%# Container.ItemIndex + 1 %></td>
                            <td class="text-center"><%# Eval("PanelName")%></td>
                            <td class="text-center"></td>
                            <td class="text-center"></td>
                            <td class="text-center"></td>
                            <td class="text-center">
                                <label class="switch switch-success-custom"><input name="stt" rel='<%# Eval("PmsId")%>' type="checkbox" <%#(int)Eval("PanelSatus")==1?"checked":"" %>><span></span></label>
                            </td>
                        </tr>
                    </asp:PlaceHolder>
                    <asp:PlaceHolder ID="phChild" runat="server" Visible="false">
                        <tr>
                            <td class="text-center"><%# Container.ItemIndex + 1 %></td>
                            <td class="text-center"><i class="fa fa-angle-double-right fa-fw"></i><%# Eval("PanelName")%></td>
                            <td class="text-center">
                                <label class="switch switch-success-custom"><input name="1" rel='<%# Eval("PmsId")%>' type="checkbox" <%#(byte)Eval("PmsView")==1?"checked":"" %>><span></span></label>
                            </td>
                            <td class="text-center">
                                <label class="switch switch-success-custom"><input name="2" rel='<%# Eval("PmsId")%>' type="checkbox" <%#(byte)Eval("PmsUpdate")==1?"checked":"" %>><span></span></label>
                            </td>
                            <td class="text-center">
                                <label class="switch switch-success-custom"><input name="3" rel='<%# Eval("PmsId")%>' type="checkbox" <%#(byte)Eval("PmsDelete")==1?"checked":"" %>><span></span></label>
                            </td>
                            <td class="text-center">
                                <label class="switch switch-success-custom"><input name="4" rel='<%# Eval("PmsId")%>' type="checkbox" <%#(int)Eval("PanelSatus")==1?"checked":"" %>><span></span></label>
                            </td>
                                
                        </tr>
                    </asp:PlaceHolder>
                </ItemTemplate>
            </asp:Repeater>
        </tbody>
    </table>
</div>
<script>
    var str = '<%=lstShortPms %>';
    var jsonObj = $.parseJSON(str);
    //console.log(jsonObj);
    $("#tblPms").find("input:checkbox").each(function () {
        var $this = $(this);
        $this.change(function () {
            if ($this.is(':checked')) 
            {
                //console.log('id: ' + $this.attr("rel") + '-- name: ' + $this.attr("name") + ' -- checked');  // checked
                setVal($this.attr("rel"), 1, $this.attr("name"));
            }
            else
            {
                //console.log('id: ' + $this.attr("rel") + '-- name: ' + $this.attr("name") + ' -- uncheck');  // unchecked
                setVal($this.attr("rel"), 0, $this.attr("name"));
            }
        });
    });

    /*action: { 1: View ;  2: Update ; 3: Delete ; 4: Status }*/
    function setVal(id, data, action) {
        $.each(jsonObj, function (index, value) {
            if (value.PmsId == id) {
                if (action == 1) { // set VIEW
                    value.PmsView = data;
                } else if (action == 2) {// set UPDATE
                    value.PmsUpdate = data;
                } else if (action == 3) {// set DELETE
                    value.PmsDelete = data;
                } else {// set STATUS
                    value.PanelSatus = data;
                }
                return;
            }
        });
    }

    $("#btnUpdatePms").bind("click", function () {
        var strJson = JSON.stringify(jsonObj);
        //console.log(strJson);
        // call ajax
        var param;
        param = {
            m: "update",
            lstPms: strJson
        };
        $.ajax({
            url: "/BackEnd/Handler/Pms.ashx",
            data: param,
            type: "POST",
            dataType: "json",
            beforeSend: function () {
                jWaiting.callWaiting();
            },
            success: function (data) {
                //console.log(data);
                if (data.Success) {
                    jWaiting.callWaitingMessSuccess('Cập nhật phân quyền thành công...', 'reload', true);
                }
                else {
                    jWaiting.callWaitingMessError('Có lỗi: ' + data.Message);
                }
            }
        });
    });
</script>
